package com.henry.imgproc;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class MedianFilterDemo {
    public static BufferedImage loadBufferedImageFromFile(String filePath) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(filePath));
        } catch (IOException e) {
            System.out.println(e.toString());
        }
        return img;
    }
    public static void writeBufferedImageToFile(BufferedImage img, String formatName, String filePath) {
        try {
            ImageIO.write(img, formatName, new File(filePath));
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    }
    public static void main(String[] args) {
        String pathPrefix = "src/main/resources/ImageProcess/";
        String inputFileName = "ship.png";
        String outputFileName = "ship_fix.png";

        BufferedImage original = loadBufferedImageFromFile(pathPrefix + inputFileName);
        BufferedImage result = null;

        if (original != null) {
            System.out.println("Image rows: " + original.getHeight());
            System.out.println("Image cols: " + original.getWidth());
            try {
            result = MedianFilter.medianFilter(original,3,3);}
            catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        else {
            System.out.println("The original image is null!");
        }

        if (result != null) {
            writeBufferedImageToFile(result, "png", pathPrefix + outputFileName);
        } else {
            System.out.println("The result image is null!");
        }
    }
}
